//
// AWS Manager - A free AWS console application demonstrating advanced concepts
// Copyright (C) 2008-2010 Sean O'Dell. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.awsmanager.common;

import com.sun.data.provider.RowKey;
import com.sun.data.provider.impl.ObjectListDataProvider;
import com.sun.webui.jsf.model.Option;
import com.xerox.amazonws.ec2.EC2Exception;
import com.xerox.amazonws.ec2.KeyPairInfo;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Sean ODell
 */
public class EC2KeyPairDataProvider extends ObjectListDataProvider {

    private ArrayList ec2KeyPairList = new ArrayList();
    private EC2Connection ec2connection;

    public EC2KeyPairDataProvider() {
        setList(ec2KeyPairList);
        setObjectType(EC2KeyPair.class);
    }

    public void load(EC2Connection ec2connection) {

        clearObjectList();

        if (ec2connection == null) {
            this.ec2connection = null;
            return;
        } else {
            this.ec2connection = ec2connection;
        }

        if ((this.ec2connection = ec2connection) == null) {
            return;
        }

        ArrayList<String> ec2Regions = ec2connection.getEc2RegionNames();
        EC2Region ec2RegionInfo = null;

        for (String ec2Region : ec2Regions) {

            ec2RegionInfo = (EC2Region) ec2connection.getEc2RegionInfo().get(ec2Region);
            List<String> params = new ArrayList<String>();

            List<KeyPairInfo> keypairs = null;
            try {
                keypairs = ec2RegionInfo.getRegionConnection().describeKeyPairs(params);
            } catch (EC2Exception ex) {
                Logger.getLogger(EC2KeyPairDataProvider.class.getName()).log(Level.SEVERE, null, ex);
            }

            if (keypairs != null) {
                for (KeyPairInfo keypair : keypairs) {
                    EC2KeyPair keyPairInfo = new EC2KeyPair();
                    keyPairInfo.setKeyRegion(ec2Region);
                    keyPairInfo.setkeyName(keypair.getKeyName());
                    keyPairInfo.setkeyFingerprint(keypair.getKeyFingerprint());
                    getList().add(keyPairInfo);
                }
            }
        }
    }

    public EC2KeyPair getKeypair(RowKey rowKey) {
        EC2KeyPair ec2KeyPair = (EC2KeyPair) getObject(rowKey);
        return ec2KeyPair;
    }

    public Option[] getKeyPairNamesOptions() {
        int i = 0;
        Option[] keyPairNamesOptions = new Option[ec2KeyPairList.size()];

        for (i = 0; i < ec2KeyPairList.size(); i++) {
            EC2KeyPair ec2KeyPair = (EC2KeyPair) ec2KeyPairList.get(i);
            keyPairNamesOptions[i] = new Option(ec2KeyPair.getkeyName());
        }
        return keyPairNamesOptions;
    }

    public Option[] getKeyPairNamesOptions(String region) {
        ArrayList<EC2KeyPair> ec2KeyPairListRegion = new ArrayList();
        for (int i = 0; i < ec2KeyPairList.size(); i++) {
            EC2KeyPair ec2KeyPair = (EC2KeyPair) ec2KeyPairList.get(i);
            if (ec2KeyPair.getKeyRegion().equals(region)) {
                ec2KeyPairListRegion.add(ec2KeyPair);
            }
        }
        Option[] keyPairNamesOptions = new Option[ec2KeyPairListRegion.size()];

        for (int i = 0; i < ec2KeyPairListRegion.size(); i++) {
            EC2KeyPair ec2KeyPair = (EC2KeyPair) ec2KeyPairListRegion.get(i);
            keyPairNamesOptions[i] = new Option(ec2KeyPair.getkeyName());
        }
        return keyPairNamesOptions;
    }

    public String createKeyPair(String keypairname, String region) {
        String statusMsg = new String();
        KeyPairInfo keypairinfo = null;
        EC2Region ec2RegionInfo = null;

        if (ec2connection == null) {
            statusMsg = statusMsg + "Please update user settings first.";
            return statusMsg;
        } else {
            ec2RegionInfo = (EC2Region) ec2connection.getEc2RegionInfo().get(region);
        }

        try {
            keypairinfo = ec2RegionInfo.getRegionConnection().createKeyPair(keypairname);
            statusMsg = statusMsg + "Key Pair Name: " + keypairinfo.getKeyName() + "\n\n";
            statusMsg = statusMsg + "Key Pair Material:\n" + keypairinfo.getKeyMaterial() + "\n";
        } catch (EC2Exception ex) {
            statusMsg = statusMsg + "Error: " + ex + "\n";
            Logger.getLogger(EC2KeyPairDataProvider.class.getName()).log(Level.SEVERE, null, ex);
        }

        return statusMsg;
    }

    public String deleteKeypairs(RowKey[] selectedRowKeys) {
        String statusMsg = new String();
        EC2Region ec2RegionInfo = null;

        if (ec2connection == null) {
            statusMsg = statusMsg + "Please update user settings first.";
            return statusMsg;
        }

        for (int i = 0; i < selectedRowKeys.length; i++) {
            RowKey rowKey = selectedRowKeys[i];
            EC2KeyPair ec2KeyPair = getKeypair(rowKey);
            ec2RegionInfo = (EC2Region) ec2connection.getEc2RegionInfo().get(ec2KeyPair.getKeyRegion());
            try {
                ec2RegionInfo.getRegionConnection().deleteKeyPair(ec2KeyPair.getkeyName());
                statusMsg = statusMsg + "Deleted Keypair: " + ec2KeyPair.getkeyName() + "\n";
            } catch (EC2Exception ex) {
                statusMsg = statusMsg + "Error: " + ex + "\n";
            }
        }
        return statusMsg;
    }
}